package jee.boot.common.basic;

import com.fasterxml.jackson.annotation.JsonInclude;
import jee.boot.common.annotation.PrimaryKey;
import jee.boot.common.annotation.Transient;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Entity支持类
 *
 * @param <T>
 * @author zhilei.wang
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class BaseEntity<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 实体编号（唯一标识）
     */
    @PrimaryKey
    protected String id;

    /**
     * 当前实体分页对象
     */
    @Transient
    protected Page<T> page;


    public BaseEntity() {
    }

    public BaseEntity(String id) {
        this();
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Page<T> getPage() {
        return page;
    }

    public Page<T> setPage(Page<T> page) {
        this.page = page;
        return page;
    }


    /**
     * 插入之前执行方法，子类实现
     */
    public abstract void preInsert();

    /**
     * 更新之前执行方法，子类实现
     */
    public abstract void preUpdate();

    /**
     * 查询之前执行方法，子类实现
     */
    public abstract void preQuery();

    /**
     * 插入之前执行方法，子类实现
     */
    public abstract void postInsert(Object data);

    /**
     * 更新之前执行方法，子类实现
     */
    public abstract void postUpdate(Object data);

    /**
     * 查询之前执行方法，子类实现
     */
    public abstract void postQuery(Object data);

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!getClass().equals(obj.getClass())) {
            return false;
        }
        BaseEntity<?> that = (BaseEntity<?>) obj;
        return null == this.getId() ? false : this.getId().equals(that.getId());
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

}

